package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.AppVersionBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TWaterCompanyBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.AppVersionMapper;
import com.koron.css2.serviceManage.mapper.TWaterCompanyMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.utils.RedisUtils;
import com.koron.css2.systemManage.dto.UserAuthorityDto;
import com.koron.css2.systemManage.service.UserService;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 切换水司
 * @author 周利斌
 *
 */
public class SwitchCompany implements ServerInterface{

	private static Logger log = LogManager.getLogger(SwitchCompany.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "执行成功", Map.class);
		try {
			String curWaterCode = JsonUtils.objectToPojo(req.getData(), String.class);
			if(StringUtils.isEmpty(curWaterCode)) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("水司不能为空");
				return msg;
			}
			Map<String, Object> data = new HashMap<>();
			TWaterCompanyMapper waterCompanyMapper = factory.getMapper(TWaterCompanyMapper.class, "_default");
			AppVersionMapper appVersionMapper = factory.getMapper(AppVersionMapper.class, "_default");
			TWaterCompanyBean oldWaterCompany = waterCompanyMapper.selectByCode(userInfo.getCurWaterCode());
			TWaterCompanyBean newWaterCompany = waterCompanyMapper.selectByCode(curWaterCode);

			AppVersionBean oldAppVersion = appVersionMapper.selectById(oldWaterCompany.getAppVersion());
			AppVersionBean newAppVersion = appVersionMapper.selectById(newWaterCompany.getAppVersion());

			userInfo.setCurWaterCode(curWaterCode);
			RedisUtils.del("user_auth_"  + req.getToken() + "_" + userInfo.getUserInfo().getAcount()); // 先删除
			RedisUtils.setEx("user_company_" + req.getToken() + "_" + userInfo.getUserInfo().getAcount(), ApplicationConfig.getExpireTime(), curWaterCode);

			UserAuthorityDto userAuthority = new UserService().getUserAuthority(factory, userInfo, ApplicationConfig.getAppCustomerAppid(), req.getToken());
			userAuthority.setCurWaterCode(curWaterCode);

			RedisUtils.setEx(req.getToken(), ApplicationConfig.getExpireTime(), JsonUtils.objectToJson(userInfo));
			RedisUtils.setEx("user_auth_"  + req.getToken() + "_" + userInfo.getUserInfo().getAcount(), ApplicationConfig.getExpireTime(), JsonUtils.objectToJson(userAuthority));

			/*if (!Objects.equals(oldAppVersion.getDomain(), newAppVersion.getDomain())){ // 假如域名不一样
				data.put("url", newAppVersion.getDomain() + "?ticket=" + req.getToken()); // 返回水司的域名并把当前的ticket返回前端
				data.put("userAuthority", "");
				msg.setData(data);
				return msg;
			}*/

			data.put("url", "");
			data.put("userAuthority", userAuthority);
			msg.setData(data);
		} catch (Exception e) {
			log.error("执行出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("执行出错");
		}
		return msg;
	}
}
